package com.cfh.springcloudclientapplication.customclient;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class RequestMappingMethodInvocationHandler implements InvocationHandler {

    private final ParameterNameDiscoverer parameterNameDiscoverer =
            new DefaultParameterNameDiscoverer();

    private final String serviceName;

    private final BeanFactory beanFactory;

    public RequestMappingMethodInvocationHandler(String serviceName, BeanFactory beanFactory) {
        this.serviceName = serviceName;
        this.beanFactory = beanFactory;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        GetMapping getMapping = AnnotationUtils.findAnnotation(method,
                GetMapping.class);
        if (getMapping != null) {
            //得到uri
            String[] uri = getMapping.value();
            //拼接url
            StringBuilder urlString = new StringBuilder("http://").append(serviceName).append(
                    "/").append(uri[0]);
            //方法参数数量
            int parameterCount = method.getParameterCount();
            //方法参数名 fixme
            String[] parameterNames = parameterNameDiscoverer.getParameterNames(method);
            //方法参数注解
            Annotation[][] parameterAnnotations = method.getParameterAnnotations();
            //查询字符串
            StringBuilder queryStringBuilder = new StringBuilder();
            for (int i = 0; i < parameterCount; i++) {
                Annotation[] parameterAnnotation = parameterAnnotations[i];
                RequestParam requestParam = (RequestParam) parameterAnnotation[0];
                if (requestParam != null) {
                    //请求参数的名称
                    String parameterName = "";
                    String requestParamName = StringUtils.hasText(requestParam.value()) ?
                            requestParam.value() : parameterName;
                    //请求参数类型
                    String requestParameterValue = String.valueOf(args[i]);
                    queryStringBuilder.append("&").append(requestParamName).append("=").append(requestParameterValue);
                }
            }
            String queryString = queryStringBuilder.toString();
            if (StringUtils.hasText(queryString)) {
                urlString.append("?").append(queryString);
            }

            String url = urlString.toString();

            RestTemplate restTemplate = beanFactory.getBean("loadBalancedRestTemplate", RestTemplate.class);
            return restTemplate.getForObject(url, method.getReturnType());
        }

        return null;
    }
}
